package com.bookmall.product.controller;

import com.bookmall.common.utils.PageUtils;
import com.bookmall.product.domain.BookCategory;
import com.bookmall.product.service.IBookCategoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-02-21-16:59
 */
@RestController
@RequestMapping("/product/category")
public class BookCategoryController extends BaseController {

    @Autowired
    private IBookCategoryService bookCategoryService;

    /**
     * 分页查询分类（可通过实体类条件）
     */
    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @Log(title = "查询列表", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public AjaxResult list(BookCategory bookCategory)
    {
        PageUtils page = bookCategoryService.selectCategoryList(bookCategory);
        return AjaxResult.success(page);
    }

    /**
     * 查询所有分类（以树形结构组装）
     * @author 胡菜鸡
     * @date 2022/2/21 22:22
     */
    @Log(title = "查询树形列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @GetMapping("/list/tree")
    public AjaxResult listWithTree(BookCategory bookCategory){
        List<BookCategory> entities = bookCategoryService.listWithTree(bookCategory);
        return AjaxResult.success(entities);
    }

    /**
     * 查询所有展示的分类（以树形结构组装）
     * @author 胡菜鸡
     * @date 2022/2/21 22:22
     */
    @Log(title = "查询树形列表", businessType = BusinessType.OTHER)
    @GetMapping("/list/treeShowing")
    public AjaxResult listWithTreeForStatus(){
        List<BookCategory> entities = bookCategoryService.listWithTreeForStatus();
        return AjaxResult.success(entities);
    }

    /**
     * 根据id获取分类列表详细信息
     * @author 胡菜鸡
     * @date 2022/2/20 18:17
     */
    @PreAuthorize("@ss.hasPermi('product:category:query')")
    @Log(title = "查询信息", businessType = BusinessType.OTHER)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(bookCategoryService.getById(id));
    }

    /**
     * 新增分类
     * @author 胡菜鸡
     * @date 2022/2/20 17:49
     */
    @PreAuthorize("@ss.hasPermi('product:category:add')")
    @Log(title = "新增分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookCategory bookCategory)
    {
        return toAjax(bookCategoryService.insertCategory(bookCategory));
    }

    /**
     * 修改分类
     * @author 胡菜鸡
     * @date 2022/2/20 18:52
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "修改分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookCategory bookCategory)
    {
        return toAjax(bookCategoryService.updateCategoryById(bookCategory));
    }

    /**
     * 修改分类展示状态
     * @author 胡菜鸡
     * @date 2022/2/20 18:52
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "分类展示状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody BookCategory bookCategory)
    {
        return toAjax(bookCategoryService.updateById(bookCategory));
    }

    /**
     * 删除分类
     * @author 胡菜鸡
     * @date 2022/2/20 19:17
     */
    @PreAuthorize("@ss.hasPermi('product:category:remove')")
    @Log(title = "删除分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(bookCategoryService.removeCategoryByIds(ids));
    }
}
